package com.wandian.core
{

	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.MovieClip;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.system.ApplicationDomain;
	import flash.utils.Dictionary;
	import flash.utils.getDefinitionByName;

	import ghostcat.display.movieclip.GMovieClip;

	/**
	 * 位图管理类
	 * @author long
	 *
	 */
	public class BitmapManager
	{
		private static var _instance:BitmapManager=null;

		private const ROLETILECOLS:int=8;
		private var _cache:Dictionary;
		private var _mcCache:Dictionary;
		/**
		 * 角色图片缓存，因为要重复利用，不能销毁，只读取
		 */
		public var roleBitmapDict:Dictionary;
		/**
		 * 技能动画字典
		 */
		public var skillDict:Dictionary;

		public function BitmapManager(prvClass:PrivateClass)
		{

			this.roleBitmapDict=new Dictionary();
			this.skillDict=new Dictionary();
			this._cache=new Dictionary(true);
			this._mcCache=new Dictionary(true);
		}

		/**
		 * 获取技能
		 * @param name
		 * @return
		 *
		 */
		public function getSkill(name:String):MovieClip
		{
			if (this.skillDict[name])
			{
				return this.skillDict[name] as MovieClip;
			}
			else
			{
				this.skillDict[name]=getMCByName(name);
				return getSkill(name);
			}
			return null;
		}

		public function getBitmapDataByName(name:String):BitmapData
		{
			var bd:BitmapData=_cache[name];
			if (bd == null)
			{
				try
				{
					var _class:Class=Class(ApplicationDomain.currentDomain.getDefinition(name));
					if (_class == null)
					{
						trace("BitmapManager getBitmapDataByName null:" + name);
						return null;
					}
					bd=new _class() as BitmapData;
					_cache[name]=bd;
				}
				catch (e:Error)
				{

				}
			}
			return bd;

		}

		public function getMCByName(name:String):MovieClip
		{
			var mc:MovieClip=_cache[name]
			if (mc == null)
			{
				var _class:Class=Class(ApplicationDomain.currentDomain.getDefinition(name));
				if (_class == null)
				{
					trace("BitmapManager getMCByName null:" + name);
					return null;
				}
				mc=new _class();
				_cache[name]=mc;
			}
			return mc;
//			if (ApplicationDomain.currentDomain.hasDefinition(name))
//			{
//				var cls:Class=ApplicationDomain.currentDomain.getDefinition(name) as Class;
//				var mc:MovieClip=new cls() as MovieClip;
//				return mc;
//			}
//			return null;
		}

		/**
		 * 位图切割成小位图数组
		 * @param source bmd源
		 * @param tileW 切块宽
		 * @param tileH 切块高
		 * @param row 第几行
		 * @return
		 *
		 */
		public function cutBitmapData(source:BitmapData, tileW:int, tileH:int, row:int):Vector.<BitmapData>
		{
			var result:Vector.<BitmapData>=new Vector.<BitmapData>();

			for (var i:int=0; i < ROLETILECOLS; i++)
			{
				var bitmapData:BitmapData=new BitmapData(tileW, tileH, true, 0);
				bitmapData.lock();
				bitmapData.copyPixels(source, new Rectangle(i * tileW, row * tileH, tileW, tileH), new Point());
				bitmapData.unlock();
				result.push(bitmapData);
			}

			return result;
		}


		/**
		 * 左右旋转bitmapData
		 * @param sourceList
		 * @return
		 *
		 */
		public function rotationBitmapData(sourceList:Vector.<BitmapData>):Vector.<BitmapData>
		{
			if (!sourceList || sourceList.length == 0)
			{
				trace("sourceList null");
				return null;
			}
			var result:Vector.<BitmapData>=new Vector.<BitmapData>();
			for each (var elem:BitmapData in sourceList)
			{
				var newBMD:BitmapData=new BitmapData(elem.width, elem.height, true, 0);
				for (var row:int=0; row < elem.width; row++)
				{
					for (var col:int=0; col < elem.height; col++)
					{
						newBMD.lock();
						newBMD.setPixel32(elem.width - row, col, elem.getPixel32(row, col));
						newBMD.unlock();
					}
				}
				result.push(newBMD);
			}

			return result;
		}


		public static function get instance():BitmapManager
		{
			if (_instance == null)
			{
				_instance=new BitmapManager(new PrivateClass());
			}
			return _instance;
		}

		/**
		 * 获取动作名字，walk or wait or...
		 * @param str
		 * @return
		 *
		 */
		private function getActionName(str:String, roleData:*):String
		{
			var result:String;
			var len:int=roleData.action.length;

			for (var index:int=0; index < len; index++)
			{
				if (str.indexOf(roleData.action[index]) != -1)
				{
					result=roleData.action[index];
				}
			}
			return result;
		}


	}
}

class PrivateClass
{
}
